#!/bin/bash

set -e

basedir=`readlink -f $(dirname $0)`

#生成alpine镜像
echo "--> 生成alpine镜像："
cd ${basedir}/alpine
if ! docker images | awk 'NR>1{printf("%s:%s\n",$1,$2)}' | grep -q 'private-alpine-glibc:3.7'; then
  docker build -t private-alpine-glibc:3.7 .
fi

#生成oracle jdk8镜像
echo "--> 生成oraclejdk8镜像："
cd ${basedir}/jdk
if ! docker images | awk 'NR>1{printf("%s:%s\n",$1,$2)}' | grep -q 'private-oracle-jdk:8-alpine'; then
  docker build -t private-oracle-jdk:8-alpine .
fi

#生成maven镜像
echo "--> 生成maven镜像："
cd ${basedir}/maven
if ! docker images | awk 'NR>1{printf("%s:%s\n",$1,$2)}' | grep -q 'private-maven:3.5.2-alpine'; then
  docker build -t private-maven:3.5.2-alpine .
fi

#生成jenkins镜像
echo "--> 生成jenkins镜像："
cd ${basedir}/jenkins
if ! docker images | awk 'NR>1{printf("%s:%s\n",$1,$2)}' | grep -q 'private-jenkins:lts-alpine'; then
  docker build -t private-jenkins:lts-alpine .
fi

#生成tomcat8镜像
echo "--> 生成jenkins镜像："
cd ${basedir}/tomcat
if ! docker images | awk 'NR>1{printf("%s:%s\n",$1,$2)}' | grep -q 'private-tomcat:8-alpine'; then
  docker build -t private-tomcat:8-alpine .
fi


#初始化目录
mkdir -p ${basedir}/gitlab/{config,data,logs}
mkdir -p ${basedir}/jenkins/jenkins_home
mkdir -p ${basedir}/nexus/nexus-data
mkdir -p ${basedir}/registry/data
mkdir -p ${basedir}/registry/auth
mkdir -p ${basedir}/jdk
mkdir -p ${basedir}/maven
mkdir -p ${basedir}/tomcat
mkdir -p ${basedir}/nginx
cd ${basedir}/
sudo chown -R 200 nexus/nexus-data

#选择是否使用默认域名
while :; do
  read -p "请输入一个域名，该域名与docker私有仓库的ssl证书生成及CI系统访问地址相关，格式(gateray.org): " domain
  if [ -z "$domain" ]; then
    echo "使用默认域名和证书，gateray.org"
    exit 0
  fi
  read -p "你输入的域名为：${domain}，重新输入请按‘r’，任意键继续: " sel
  if [ "$sel" != "r" ] && [ "$sel" != "R" ]; then
    break
  fi
done

#修改所有配置中的域名
echo "--> 修改配置文件中的域名："
cd ${basedir}
sed -i "s/\.gateray\.org/.${domain}/g" docker-stack-compose.yml
sed -i "s/\.gateray\.org/.${domain}/g" docker-compose-gitlab.yml
sed -i "s/\.gateray\.org/.${domain}/g" nginx/conf/conf.d/*.conf

#生成registry的ssl证书
echo "--> 生成docker私有仓库ssl证书:"
while :; do
  read -p "请确认是(y)否(n)需要生成ssl证书: " isSSL
  if [ "$isSSL" = "y" ] || [ "$isSSL" = "Y" ]; then
    cd ${basedir}/nginx/conf/conf.d/server
    openssl genrsa -out server.key 2048
    openssl req -new -key server.key -out server.csr
    echo "***********根证书私钥密码为:123456"
    sleep 2
    openssl x509 -req -days 1800 -sha1 -extensions v3_req -CA ../ca/root.crt -CAkey ../ca/root.key -CAserial root.srl -CAcreateserial -in server.csr -out server.pem
    break
  elif [ "$isSSL" = "n" ] || [ "$isSSL" = "N" ]; then
    break
  fi
done

#添加私有仓库认证用户
echo "--> 添加私有仓库认证用户："
cd ${basedir}
while :; do
  read -p "请确认是(y)否(n)继续添加用户: " isAuth
  if [ "$isAuth" = "y" ] || [ "isAuth" = "Y" ]; then
    read -p "请输入用户名：" username
    read -p "请输入密码：" password
    docker run --rm --entrypoint htpasswd registry:2.1.1 -Bbn $username $password >> registry/auth/htpasswd
  else
    break
  fi
done

#启动gitlab容器
echo "--> 启动gitlab"
cd ${basedir}
docker-compose -f docker-compose-gitlab.yml up -d

#启动CI系统容器
echo "--> 启动CI系统"
cd ${basedir}
docker stack deploy -c docker-stack-compose.yml webservices

echo finish.
